<?php

namespace app\controllers;

use Yii;
use app\models\FamilymemberModel;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\components\Check;
use app\models\FamilyinfoModel;


/**
 * FrontfamilyController implements the CRUD actions for FamilymemberModel model.
 */
class FrontfamilyController extends Controller
{
	public $layout='front';
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all FamilymemberModel models.
     * @return mixed
     */
    public function actionIndex()
    {
    	if(Check::check()){
    		$id=\Yii::$app->session->get('stu')['stuid'];
    		$familyModel=FamilyinfoModel::find()->where(['stu_id'=>$id])
    		->one();
        return $this->render('index', [
        	'id'=>$id,
        	'familyModel'=>$familyModel,
        ]);}
        else
        	return $this->redirect(\Yii::getAlias('@web').'/front/login');
    }

    /**
     * Displays a single FamilymemberModel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	if(Check::check()){
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);}
        else
        	return $this->redirect(\Yii::getAlias('@web').'/front/login');
    }

    /**
     * Creates a new FamilymemberModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
    	if(Check::check()){
    		$fid=\Yii::$app->session->get('stu')['stuid'];
        $model = new FamilymemberModel();        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            	'fid'=>$fid,
            ]);
        }
    	}
    	else
    		return $this->redirect(\Yii::getAlias('@web').'/front/login');   		
    }

    /**
     * Updates an existing FamilymemberModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
    	if(Check::check()){
    		$fid=\Yii::$app->session->get('stu')['stuid'];
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            		'fid'=>$fid
            ]);
        }
    	}
    	else
    		return $this->redirect(\Yii::getAlias('@web').'/front/login');
   	}

    /**
     * Deletes an existing FamilymemberModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
    	if(Check::check()){
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
        }
        else
        	return $this->redirect(\Yii::getAlias('@web').'/front/login');
      }

    /**
     * Finds the FamilymemberModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return FamilymemberModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
    	if(Check::check()){
        if (($model = FamilymemberModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
        }
        else
        	return $this->redirect(\Yii::getAlias('@web').'/front/login');      	 
    }
    
    
    public function actionCreateinfo()
    {
    	if(Check::check()){
    		$model=new FamilyinfoModel();
    		$id=\Yii::$app->session->get('stu')['stuid'];
    		if ($model->load(Yii::$app->request->post()) && $model->save()) {
    			return $this->redirect('index');
    		}
    		return $this->render('createinfo',[
    				'model'=>$model,
    				'id'=>$id,
    		]);
    	}
    	else
    		return $this->redirect(\Yii::getAlias('@web').'/front/login');
    }
    
    
    public function actionUpdateinfo()
    {
    	if(Check::check()){
    		$id=\Yii::$app->session->get('stu')['stuid'];
    		$model = FamilyinfoModel::find()->where(['stu_id'=>$id])->one();
    		if ($model->load(Yii::$app->request->post()) && $model->save()) {
    			return $this->redirect(['index']);
    		} else {
    			return $this->render('updateinfo', [
    					'model' => $model,
    					'id'=>$id
    			]);
    		}
    	}
    	else
    		return $this->redirect(\Yii::getAlias('@web').'/front/login');
    }
    
    
}
